<?php
namespace Badmins\Model;
use Think\Model;
class GoodsModel extends Model{
	protected $_validate = array(
		array('title','require','商品名称不能为空'),
		array('category_id','Ckcategoryid','商品分类必须',1,'callback',3),
		array('photo','Ckphoto','商品图片必须',1,'callback',3),
	);
	protected $_auto = array (
		array ('photo','Ckphoto',3,'callback',1),
		array('img','Ckimg',3,'callback',1),
	);
	/**
	 * 获取第一张图片为主图
	 */
	public function Ckimg(){
		$photo = I('photo');
		return $photo[0];
	}
	/**
	 * 判断图片是否上传
	 * @return boolean
	 */
	public function Ckphoto(){
		$photo = I('photo');
		if(empty($photo)){
			return false;
		}else{
			return join(",",$photo);
		}
	}
	/**
	 * 判断是否选择分类
	 */
	public function Ckcategoryid(){
		$categoryid = I('category_id');
		if(empty($categoryid)){
			return false;
		}else{
			return true;
		}
	}
	/**
	 * 添加到商品规格
	 */
	public function save_spec($goods_id){
		$goods_spec_array = $_POST['goods_spec_array'];
		if(!empty($goods_spec_array)){
			$db6 = M('goods_spec');
			$re = $db6->where("goods_id='{$goods_id}'")->delete();
			$goods_spec_array = json_decode($goods_spec_array,true);
			foreach ($goods_spec_array as $b=>$vb){
				$spec['spec_id'] = $vb['id'];
				$spec['goods_id'] = $goods_id;
				$spec['spec_name'] = $vb['spec_array']['name'];
				$spec_value[$b] = explode(",",$vb['spec_array']['value']);
				foreach ($spec_value[$b] as $f=>$vf){
					$spec['spec_value'] = $vf;
					$db6->add($spec);
				}
			}
		}
	}
	/**
	 * 添加到商品属性
	 */
	public function save_attr($goods_id){
		$attr_name = I('attr_name');//属性
		$attr_value = I('attr_value');//属性值
		//添加到商品属性
		if(!empty($attr_name)){
			$db5 = M('goods_attribute');
			$db5->where("goods_id='{$goods_id}'")->delete();
			foreach ($attr_name as $m=>$vm){
				if(!empty($attr_value[$m])){
					$attr['attr_id'] = $m;
					$attr['attr_name'] = $vm[0];
					$attr['attr_value'] = $attr_value[$m][0];
					$attr['goods_id'] = $goods_id;
					$db5->add($attr);
				}
			}
		}
	}
	/**
	 * 添加到商品分类
	 */
	public function save_category($goods_id){
		$categoryid = I('category_id');
		//添加分类
		$db1 = M('category_extend');
		$db1->where("goods_id='{$goods_id}'")->delete();
		foreach ($categoryid as $va){
			$data['goods_id'] = $goods_id;
			$data['category_id'] = $va;
			$db1->add($data);
		}
	}
	/**
	 * 添加到商品推荐类型
	 */
	public function save_commend($goods_id){
		$commend_id = I('commend_id');//推荐类型
		//添加推荐类型
		if(!empty($commend_id)){
			$db2 = M('commend_goods');
			$db2->where("goods_id='{$goods_id}'")->delete();
			foreach ($commend_id as $vaa){
				$map['goods_id'] = $goods_id;
				$map['commend_id'] = $vaa;
				$db2->add($map);
			}
		}
	}
	/**
	 * 添加到所属城市列表
	 */
	public function save_city($goods_id){
		$city = I('city_id');
		if(!empty($city)){
			$db7 = M('city_goods');
			$db7->where("goods_id='{$goods_id}'")->delete();
			$cityarea = array();
			foreach ($city as $vas){
				$cityarea['goods_id'] = $goods_id;
				$cityarea['city_id'] = $vas;
				$cityarea['city_level'] = M('city')->where("id='{$vas}'")->getField('level');
				$db7->add($cityarea);
			}
		}
	}
	/**
	 * 添加到产品
	 */
	public function save_product($goods_id){
		//添加产品
		$product_id = I('product_id');
		$spec_array = I('spec_array');//规格
		$group = I('group');//会员组价格
		$products_no = I('products_no');//产品号
		$code = I('code');//条形码
		$market_price = I('market_price');//市场价格
		$sell_price = I('sell_price');//销售价格
		$cost_price = I('cost_price');//成本价格
		$integral = I('integral');//可获得的积分
		$weight = I('weight');//产品的重量
		$commission = I('commission');//分佣的价格
		$store_nums = I('store_nums');//库存
		$photo_list = I('photo_list');//图片
		$db3 = M('products');
		if(!empty($product_id[0])){
			$re = $db3->where("goods_id='{$goods_id}'")->field("id")->select();
			foreach ($re as $b=>$vl){
				if(in_array($vl['id'],$product_id)){
				}else{
					$db3->where("id='{$vl['id']}'")->delete();
				}
			}
		}
		foreach ($sell_price as $l=>$vl){
			$where['goods_id'] = $goods_id;
			if(!empty($spec_array[$l])){
				$where['spec_array'] = $spec_array[$l];
			}
			$where['products_no'] = $products_no[$l]?$products_no[$l]:goods_no($l+1);
			$where['store_nums'] = $store_nums[$l]?$store_nums[$l]:defalut_store();
			$where['market_price'] = $market_price[$l];
			$where['sell_price'] = $vl;
			$where['cost_price'] = $cost_price[$l];
			$where['integral'] = $integral[$l];
			$where['weight'] = $weight[$l];
			/* $where['commission'] = $commission[$l]; */
			$where['photo_list'] = $photo_list[$l];
			$where['code'] = $code[$l];
			if($product_id[$l]){
				$where['id'] = $product_id[$l];
				$db3->save($where);
				$products_id[$l] = $product_id[$l];
			}else{
				$products_id[$l] = $db3->add($where);
			}
			if($group[$l]){
				$grouparray[$l] = explode(',',$group[$l]);
				$db4 = M('group_price');
				$db4->where("goods_id='{$goods_id}' and product_id='{$products_id[$l]}'")->delete();
				foreach ($grouparray[$l] as $u=>$vu){
					$grouplist['goods_id'] = $goods_id;
					$grouplist['product_id'] = $products_id[$l];
					list($grouplist['group_id'],$grouplist['price']) = explode("_",$vu);
					$dis[$u] = M('user_group')->where("id='{$grouplist['group_id']}'")->getField('discount');
					$grouplist['price'] = $dis[$u]/100*$grouplist['price'];
					$db4->add($grouplist);
				}
			}
		}
	}
}